package com.szbmyoubao.app.ui.main.rank

import android.graphics.Color
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.szbmyoubao.app.ui.main.rank.good.GoodFragment
import com.ellecity06.common.base.BaseMvvmFragment
import com.ellecity06.cwhiskymall.databinding.FragmentClassifyBinding
import com.google.android.material.tabs.TabLayoutMediator
import kotlin.math.abs

/**
 * @author ellecity06
 * @time 2024/10/22 14:53
 * @des 豹榜
 */
class RankFragment : BaseMvvmFragment<FragmentClassifyBinding, RankViewModel>() {
    private val strArray = arrayOf("食品饮料", "家具百货", "服饰内衣","酒品")

    companion object {
        @JvmStatic
        fun newInstance(): RankFragment {
            return RankFragment()
        }

    }

    override fun lazyInit() {
//            这种用法当fragment不会有内存泄露 虽然fragment很多 但是每次都是用的RecyclerFragment.newInstance()
        binding.viewPager2.adapter = object : FragmentStateAdapter(this) {
            override fun getItemCount(): Int = strArray.size

            override fun createFragment(position: Int): Fragment {
                return GoodFragment.Companion.newInstance(1)
            }
        }
        TabLayoutMediator(binding.tabLayout, binding.viewPager2) { tab, position ->
            tab.text = strArray[position]
        }.attach()
    }

    override fun initView() {

        binding.appbar.addOnOffsetChangedListener { appbarLayout, offset ->
            val verticalHeight = abs(offset)
            if (verticalHeight == binding.appbar.totalScrollRange) {
                binding.statusBar.setBackgroundColor(Color.WHITE)
            } else {
                binding.statusBar.setBackgroundColor(Color.TRANSPARENT)
            }
        }
    }

    override fun inflateBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): FragmentClassifyBinding {
        return FragmentClassifyBinding.inflate(inflater, container, false)
    }

}